package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.Item;
import com.njtech.pet.entity.SingleRecord;
import com.njtech.pet.service.ItemService;
import com.njtech.pet.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:cw
 * Date:2020-04-19 22:30
 * Description:<描述>统计模块相关的功能
 */
@Controller
@RequestMapping("/statistic")
public class StatisticController {
    @Autowired
    private StatisticService statisticService;

    @Autowired
    private ItemService itemService;

    @RequestMapping("/total")
    @ResponseBody
    public String totalNumber(){
        Map<String,Integer> map = new HashMap<>();
        Integer userNum = statisticService.showTotalUserNum(false);
        Integer memberNum = statisticService.showTotalUserNum(true);
        Integer itemNum = statisticService.showTotalItemNum(null);
        Integer billNum = statisticService.showTotalBillNum(null);
        map.put("userNum",userNum);
        map.put("memberNum",memberNum);
        map.put("itemNum",itemNum);
        map.put("billNum",billNum);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/graph")
    @ResponseBody
    public String numberInWeek(){
        Map<String,List<Integer>> map = new HashMap<>();
        List<Integer> userList = statisticService.showActiveUserOrMemberNumInWeek(false);
        List<Integer> itemList = statisticService.showItemNumInWeek(null,null,null);
        List<Integer> billList = statisticService.showBillNumInWeek(null, null);
        List<Integer> memberList = statisticService.showActiveUserOrMemberNumInWeek(true);
        map.put("userList",userList);
        map.put("itemList",itemList);
        map.put("billList",billList);
        map.put("memberList",memberList);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/sectorChart")
    @ResponseBody
    public String numberOfItemByType(){
        Map<String,List<String>> map = new HashMap<>();
        //按照商品类型，显示新增保险
        List<String> list = itemService.selectAllType();
        map.put("typeList",list);
        List<Item> temList;
        List<String> numList = new ArrayList<>();
        Item item = new Item();
        for(int i = 0;i < list.size();i++){
            item.setType(list.get(i));
            temList = itemService.selectElastically(item,null,null,null,null,null,null);
            numList.add(i,""+temList.size());
        }
        map.put("numList",numList);
        return JSON.toJSONString(map);
    }

    @RequestMapping("/barGraph")
    @ResponseBody
    public String numberOfBIllByMonth(){
        Map<String,List<SingleRecord>> map = new HashMap<>();
        List<SingleRecord> itemList = statisticService.showItemNumByMonth(12, null, null, null);
        List<SingleRecord> billList = statisticService.showBillNumByMonth(12, null, null);
        map.put("itemList",itemList);
        map.put("billList",billList);
        return JSON.toJSONString(map);

    }
}
